<?php
namespace app\appoint\model;
use app\BaseModel;
use think\facade\Db;



class QuestionUserText extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_question_user_text';
    /**
     * @param $query
     * 查询范围
     */
    public function scopeStatus($query, $status){

        $query->where('status', '>', $status);
    }


    /**
     * @param $value
     * @param $data
     * @功能说明:转换时间格式
     * @author chenniang
     * @DataTime: 2021-01-28 10:12
     */
    public function getCreateTimeAttr($value,$data){

        if(!empty($value)){

            return date('Y-m-d H:i:s',$value);
        }

    }
    /**
     * @param $dis
     * @param $page
     * @功能说明:问卷列表
     * @author chenniang
     * @DataTime: 2021-01-25 15:56
     */
    public function dataList($dis,$page){

        $data = $this->where($dis)->status(-1)->order('top desc')->paginate($page)->toArray();

        return $data;
    }


    /**
     * @author chenniang
     * @DataTime: 2021-01-26 10:57
     * @功能说明:用户文券列表
     */
    public function userTextList($dis,$page=10){

        $data = $this->alias('a')
                ->join('longbing_card_user b','a.user_id = b.id')
                ->where($dis)
                ->field(["a.*","b.nickName","b.avatarUrl"])
                ->group('a.id')
                ->order('a.id desc')
                ->paginate($page)
                ->toArray();

        if(!empty($data['data'])){

            foreach ($data['data'] as $k=>$v){

                $data['data'][$k]['avatarUrl'] = !empty($v['avatarUrl'])?$v['avatarUrl']:'https://lbqnyv2.migugu.com/defaultAvatar.png';

            }
        }

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-01-26 10:57
     * @功能说明:用户文券列表
     */
    public function userTextSelect($dis){

        $data = $this->alias('a')
            ->join('longbing_card_user b','a.user_id = b.id')
            ->where($dis)
            ->field(["a.*","b.nickName","ifnull(b.avatarUrl,'https://lbqnyv2.migugu.com/defaultAvatar.png') as avatarUrl"])
            ->group('a.id')
            ->order('a.id desc')
            ->select()
            ->toArray();
        return $data;

    }

    /**
     * @author chenniang
     * @DataTime: 2021-01-26 11:03
     * @功能说明:问卷详情
     */
    public function userTextInfo($order_id){

        $list_model = new QuestionList();

        $data = $list_model->dataSelect(['order_id'=>$order_id]);

        return $data;
    }

    /**
     * @param $data
     * @功能说明:添加
     * @author chenniang
     * @DataTime: 2021-01-25 15:57
     */
    public function dataAdd($data){

        $res = $this->insert($data);

        return $res;
    }

    /**
     * @param $dis
     * @param $data
     * @功能说明:编辑
     * @author chenniang
     * @DataTime: 2021-01-25 15:57
     */
    public function dataUpdate($dis,$data){

//        $data['update_time'] = time();

        $res = $this->where($dis)->update($data);

        return $res;

    }

    /**
     * @param $dis
     * @功能说明:详情
     * @author chenniang
     * @DataTime: 2021-01-25 15:57
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->status(-1)->find();

        return !empty($data)?$data->toArray():$data;

    }





}